Option Strict Off
Option Explicit On
Module AVIAppendJPG
	
	'This module creates and appends Motion Jpeg files when supplied with jpeg images
	'The main call is to the AppendMJpeg routine, which handles creation and appending the file.
	'Created by Martin Price December 2008. www.HeliumFrog.com
	
	
	
	'This is a structure to hold the index data at the end of the AVI file it is based on a AVIOLDSTRUCTURE see MSDN website
	Private Structure INDEXSTRUCT
		Dim dwChunkid As Integer 'Holds the idx1 identifier
		Dim dwFlags As Integer 'Various flag data (always is set to decimal 16)
		Dim dwOffset As Integer 'The offset of the data from the start of the file
		Dim dwSize As Integer 'The size of the data chunk
	End Structure
	
	Dim IndexBlock As INDEXSTRUCT 'An instance of the above structure
	
	
	Private Structure FRAMEDATA 'A Data structure that holds 1 jpeg raw frame data and also the header and length of data
		Dim lngMarker As Integer 'The 00db marker
		Dim lngSize As Integer 'The length of the following data
		Dim RawJpgData() As Byte 'The raw jpeg frame data
	End Structure
	
	'UPGRADE_WARNING: Arrays in structure JpgData may need to be initialized before they can be used. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="814DF224-76BD-4BB4-BFFB-EA359CB9FC48"'
	Dim JpgData As FRAMEDATA 'An instance of the above structure
	
	'UPGRADE_WARNING: Arrays in structure JpgDataEmpty may need to be initialized before they can be used. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="814DF224-76BD-4BB4-BFFB-EA359CB9FC48"'
	Dim JpgDataEmpty As FRAMEDATA 'An empty structure that is used to clear the datastructure above
	
	Dim RetrievedData As Integer 'A long to hold any data that has been retrieved
	Dim FrameCount As Integer 'The number of frames in the file (before modification)
	Dim IndexStart As Integer 'the start of the index file. "the line that contains "idx1"
	Dim OffsetDown As Integer 'The amount to move the index data down for the new jpeg data to fit before it
	Dim i As Integer
	Dim ByteValue As Byte 'A variable to hold a single byte, used throughout the module as a temporary store
	Dim DataGet1 As Byte 'As above
	Dim DataGet2 As Byte 'As above
	Dim DataLong1 As Integer 'Temporary store for a long value
	Dim DataLong2 As Integer 'Temporary store for a long value
	Dim JPegDataEnd As Integer 'Holds the end point of the jpeg data
	Dim JPegDataLength As Integer 'Holds the total length of the jpeg data, between the movi line and the idx1 line
	Dim ByteAdded As Boolean 'A flag to show whether a single padding byte has been added to the data length.
	Dim ErrorCode As Integer 'Holds the error code that has been found
	
	Public Function AppendMJpeg(ByRef SaveFilename1 As String, ByRef JPegName As String, ByRef FramesPerSecond As Integer, ByRef FrameWidth1 As Integer, ByRef FrameHeight1 As Integer) As Boolean
		On Error GoTo ErrorHandling 'If an error occurs go to the error handling routine
		
		Dim MJFileLengthCheck As Double 'This is used to check if the .avi file length of 2GB is going to be exceeded.
		
		AppendMJpeg = False 'Set the function to initially return false
		
		ErrorCodeNo = "03.00" 'Set an error code for the next section of program
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SaveFilename1) = "" Then 'Check if the file exists
			Call CreateMJpgHead(SaveFilename1, FramesPerSecond, FrameWidth1, FrameHeight1) 'Call the routine to create the Motion Jpeg header
		End If
		
		ErrorCodeNo = "03.01" 'Set an error code for the next section of program
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(JPegName) = "" Then 'Check if the jpeg file exists
			Call MsgBox("Cannot locate the jpeg file to append to the avi. file", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			FileClose(3) 'Close the motion jpeg file if open
			FileClose(6) 'Close the index file if open
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file if it exists
			Exit Function 'This is a non fatal error so just exit the function
		End If
		
		ErrorCodeNo = "03.02" 'Set an error code for the next section of program
		'Check that adding this file wont go over the .avi file limit of 2GB
		FileOpen(3, SaveFilename1, OpenMode.Binary, OpenAccess.Read) 'Open The destination file
		FileOpen(8, JPegName, OpenMode.Binary, OpenAccess.Read) 'Open the source jpeg file
		MJFileLengthCheck = CDbl(CDbl(LOF(3)) + CDbl(LOF(8)) + CDbl(100)) 'Calculate the expected length of the new file (add 100 bytes for good measure!)
		FileClose(8)
		FileClose(3)
		If MJFileLengthCheck > CDbl(CDbl(2) * CDbl(1024) * CDbl(1024) * CDbl(1024)) Then
			Call MsgBox("Adding frame this frame would take us beyond the .avi file limit of 2GB - Please begin a separate file", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			Exit Function
		End If
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase any previous file that holds the temporary index data
		
		ByteAdded = False 'No padding byte has been added yet
		
		ErrorCodeNo = "03.03" 'Set an error code for the next section of program
		FileOpen(3, SaveFilename1, OpenMode.Binary) 'Open The destination file
		
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(3, FrameCount, 140 + 1) 'Get the number of frames already in the file
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(3, DataLong1, 64 + 1) 'Get image width from file
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(3, DataLong2, 68 + 1) 'Get image height from file
		
		If (DataLong1 <> FrameWidth1) Or (DataLong2 <> FrameHeight1) Then 'Check that the incoming jpeg size matches the file image size
			Call MsgBox("You cannot add a " & FrameWidth1 & "x" & FrameHeight1 & " image to a " & DataLong1 & "x" & DataLong2 & " Avi file!" & Chr(13) & "Frame not captured", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			FileClose(3) 'Close the motion jpeg file if open
			FileClose(6) 'Close the index file if open
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file if it exists
			Exit Function 'This is a non fatal error so just exit the function
		End If
		
		'Find the start of the index data
		If FrameCount = 0 Then 'Check if there are no frames in the file
			IndexStart = -99999999 'If no frames in file set the index to a non valid value
		Else
			IndexStart = LOF(3) - (16 * FrameCount) - 8 'Calculate where the "idx1" index start line is
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(3, RetrievedData, IndexStart + 1) 'Get the data from this line
			If RetrievedData <> &H31786469 Then 'Check that the line contains "idx1"
				Call MsgBox("Cannot locate the index line idx1" & Chr(13) & "Frame not captured", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
				FileClose(3) 'Close the motion jpeg file if open
				FileClose(6) 'Close the index file if open
				'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file if it exists
				Exit Function 'This is a non fatal error so just exit the function
			End If
		End If
		
		ErrorCodeNo = "03.04a" 'Set an error code for the next section of program
		FileOpen(6, My.Application.Info.DirectoryPath & "\IndexStore.txt", OpenMode.Binary) 'Open The file where the index data is to be stored
		ErrorCodeNo = "03.04b" 'Set an error code for the next section of program
		If FrameCount > 0 Then 'Check the file has at least one frame in it and hence an index structure
			'The file has at least one frame in it
			ErrorCodeNo = "03.04c" 'Set an error code for the next section of program
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(3, CInt(16 * (FrameCount + 1)), IndexStart + 1 + 4) 'Before saving the index, set the size of the structure to 16 bytes bigger (this is the line below the idx1 line)
			For i = IndexStart + 1 To IndexStart + ((FrameCount * 16) + 8) 'Loop through all the index bytes and copy them to the index store
				'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				FileGet(3, ByteValue, i) 'Get the value from the file at the old location
				'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				FilePut(6, ByteValue) 'Copy it to the index store file.
				'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				FilePut(3, CByte(0), i) 'Write a zero where the data was in the original file to avoid any possible errors
			Next i
		Else
			ErrorCodeNo = "03.04d" 'Set an error code for the next section of program
			'The file has no frames in it and hence no index structure so create the index header in the temporary file
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(6, CInt(&H31786469), 1) 'Place the "idx1" index header in the index store
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(6, CInt(16), 5) 'Place the length of the index structure (always 16 bytes with the first frame)
		End If
		
		ErrorCodeNo = "03.05" 'Set an error code for the next section of program
		If FrameCount > 0 Then 'Check the file has frames in it
			'The file has at least one frame and hence did have an index structure and will have some jpeg data in it
			For JPegDataEnd = LOF(3) - (FrameCount * 16) To 2 Step -1 'Step back through the file starting at the top of the index structure
				'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				FileGet(3, DataGet1, JPegDataEnd) 'Get the first byte
				'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
				FileGet(3, DataGet2, JPegDataEnd - 1) 'Get the byte just before it
				If DataGet1 = CByte(&HD9) And DataGet2 = CByte(&HFF) Then 'Check if the data here is a "d9" and "ff" denoting the end of the last jpeg data
					GoTo IveFoundJpegEnd1 'Ok we've found the end of the jpeg, so exit loop. This leaves the position we want in the JPegDataEnd variable
				End If
			Next JPegDataEnd
			'If we get here weve gone through all the file and not found a valid end to the jpeg data.
			Call MsgBox("Cant find the end of the jpeg data in Avi file" & Chr(13) & "Frame not captured", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "Helium Frog Animator")
			FileClose(3) 'Close the motion jpeg file if open
			FileClose(6) 'Close the index file if open
			'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file if it exists
			Exit Function 'This is a non fatal error so just exit the function
		Else
			'The file has no frames in it and hence no index structure
			JPegDataEnd = 2560 'No frames yet, so you should always start writing the next frame at 2560
		End If
IveFoundJpegEnd1: 
		
		ErrorCodeNo = "03.06" 'Set an error code for the next section of program
		Call GetJpgFrame(JPegName) 'Get the data from the New Jpeg frame
		
		ErrorCodeNo = "03.07" 'Set an error code for the next section of program
		JPegDataLength = CInt(JPegDataEnd + JpgData.lngSize + 8 - 2556 + 2) 'We can now calculate what the new total jpeg data length will be
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, JPegDataLength - 1, 2552 + 1) 'Store the total length of the frame data in the avi file
		
		'Set index structure for the new frame
		IndexBlock.dwChunkid = CInt(&H62643030) 'Index block begins with "00db"
		IndexBlock.dwFlags = CInt(16) 'This is data flags (keyframes etc.) always set this to 16
		IndexBlock.dwOffset = JPegDataEnd 'Offset of jpeg frame data from file start in bytes (This is the point where the index used to be)
		IndexBlock.dwSize = JpgData.lngSize 'The length of the jpeg frame data in bytes (retrieved from the Jpeg frame by GetJpgFrame routine)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(6, IndexBlock) 'Write the new frame index data at the end of the index store file
		FileClose(6) 'Close the index store file
		
		If FrameCount > 0 Then
			'The file has at least one frame so we need to calculate the start position
			JPegDataEnd = CInt(2556 + JPegDataLength - JpgData.lngSize - 8 - 2) 'We now calculate where the new data will start
		Else
			'The file has no jpeg data in it, so the jpeg data will always start at 2560as set previously
		End If
		'Begin writing the new jpeg frame, positioned at the end of the last data
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, JpgData.lngMarker, JPegDataEnd + 1) 'Write the 00db header just after where the data has been found
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, JpgData.lngSize) 'Write the data size as retrieved by the GetJpgFrame routine)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, JpgData.RawJpgData) 'Write the raw jpeg data immediately after
		
		
		'check here whether the file length is exactly divisible by 2 (Format states it must be rounded to a WORD boundary ie 2 bytes)
		If LOF(3) Mod 2 <> 0 Then 'Divide the length by 2 and find the remainder
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(3, CByte(1)) 'The data length is not divisable exactly by 2 so add a padding byte (has value of 01 for some reason)
			' Debug.Print "1 Padding Byte Added"
			ByteAdded = True 'Set the flag to say that a padding byte has been added
		Else
			' Debug.Print "No Padding Bytes Added"
			ByteAdded = False 'Set the flag that no adding bytes have been added
		End If
		
		ErrorCodeNo = "03.08" 'Set an error code for the next section of program
		FileOpen(6, My.Application.Info.DirectoryPath & "\IndexStore.txt", OpenMode.Binary) 'ReOpen The file where the index data is to be stored
		For i = 1 To LOF(6) 'write the new index data from the IndexStore file to the end of the Motion Jpeg file
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(6, ByteValue, i) 'Get the value from the index file
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(3, ByteValue) 'Write it to the end of the file
		Next i
		FileClose(6) 'Close the index store file
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file as it is no longer needed
		
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(LOF(3) - 8), 4 + 1) 'Set the total length of the file in header. This is total file length minus the first 8 bytes
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(1000000 / FramesPerSecond), 32 + 1) 'Frames per second shows microseconds per frame (the user may have changed this in the settings window during animation)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(FrameCount + 1), 48 + 1) 'Set the total number of frames in header (Not actually required for file to work)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(1000000 / FramesPerSecond), 128 + 1) 'dwScale (the user may have changed the frames per second in the settings window during animation)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(FrameCount + 1), 140 + 1) 'Set the total number of frames in the AVIStreamheader structure (This is required)
		'   FrameNum = CLng(FrameCount + 1)
		If ByteAdded = False Then 'If a byte has been added to the file modify the data length
			'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FileGet(3, JPegDataLength, 2552 + 1)
			'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
			FilePut(3, JPegDataLength - 1, 2552 + 1)
		End If
		FileClose(3)
		AppendMJpeg = True 'The function has worked so return true to the main routine
		
		ErrorCodeNo = "03.99" 'Set an error code for the next section of program
		
		On Error GoTo 0 'Set error handling so it doesnt return here.
		Exit Function
		
		'***********************************************************************************************************************************************
ErrorHandling: 
		
		AppendMJpeg = False 'An error code has been generated, so return false too main program
		FileClose(3) 'Close the motion jpeg file if open
		FileClose(6) 'Close the index file if open
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(My.Application.Info.DirectoryPath & "\IndexStore.txt") <> "" Then Kill((My.Application.Info.DirectoryPath & "\IndexStore.txt")) 'Erase the index store file if it exists
		
		ErrorDescription = "Append JPG Problem"
		Call frmErrorBox.ErrorMessageBox() 'Put up a message Box
		Call ExitProgram() 'Exit the Program gracefully
		
	End Function
	
	Private Sub GetJpgFrame(ByRef SourceFileName As String) 'Extracts the raw data from a jpeg file and puts it in the JpgData structure.
		Dim JpgLength1 As Integer
		'UPGRADE_WARNING: Couldn't resolve default property of object JpgData. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		JpgData = JpgDataEmpty 'Set the user defined type to the empty dummy type (effectively erases any data in it)
		FileOpen(8, SourceFileName, OpenMode.Binary, OpenAccess.Read) 'Open the source jpeg file
		JpgLength1 = LOF(8)
		ReDim JpgData.RawJpgData(JpgLength1 - 1) 'Redimension the byte array to be able to hold all the jpg data from image file
		'UPGRADE_WARNING: Get was upgraded to FileGet and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FileGet(8, JpgData.RawJpgData, 1) 'This pulls the data for the image starting at first record and saves data to the byte array
		JpgData.lngMarker = &H62643030 'Set the "00db" marker. Each jpeg data stream starts with this marker
		JpgData.lngSize = CInt(LOF(8)) 'Store the length of the jpeg data stream in the header
		FileClose(8) 'Close the jpeg file
	End Sub
	Private Sub CreateMJpgHead(ByRef SaveFileName As String, ByRef FramesPerSecond As Integer, ByRef FrameWidth2 As Integer, ByRef FrameHeight2 As Integer) 'Creates an empty motion jpeg file ready for appending to
		
		'UPGRADE_WARNING: Dir has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		If Dir(SaveFileName) <> "" Then Kill((SaveFileName)) 'Erase any previous file
		FileOpen(3, SaveFileName, OpenMode.Binary) 'Open The destination File
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "RIFF", 0 + 1) 'Put the RIFF Header in place
		'   Put #3, 4 + 1, 6044                   'TOTAL FILE LENGTH. SET THIS LATER
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "AVI ", 8 + 1) 'Put the Avi Header line in place
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "LIST", 12 + 1) 'Put the LIST Header in place
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 228, 16 + 1) 'Unknown!! probably length of this structure (up to junk file)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "hdrl", 20 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "avih", 24 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 56, 28 + 1) 'Size of this structure (always 56 bytes)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(1000000 / FramesPerSecond), 32 + 1) 'Frames per second shows microseconds per frame hex 0007a120 = decimal 500000
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 10000000, 36 + 1) 'Maximum play rate in bytes per second (estimate it at FPS x JpgLength). Seems to be always set to 10 million
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 40 + 1) 'Always zero
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 65552, 44 + 1) 'Flags
		' Put #3, 48 + 1, 0                     'TOTAL NUMBER OF FRAMES IN FILE. SET THIS LATER (Not actually required)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 52 + 1) 'Initial Frames . For non interleaved data this is zero
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 1, 56 + 1) 'The number of streams (only video so set to 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 60 + 1) 'Suggested buffer size (should be set, but may be zero)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, FrameWidth2, 64 + 1) 'Image width
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, FrameHeight2, 68 + 1) 'Image Height
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 72 + 1) 'Reserved (always zero)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 76 + 1) 'Reserved (always zero)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 80 + 1) 'Reserved (always zero)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 84 + 1) 'Reserved (always zero)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "LIST", 88 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 152, 92 + 1) 'Unknown!!
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "strl", 96 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "strh", 100 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 64, 104 + 1) 'Size of following structure hex 00000040 = decimal 64 bytes
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "vids", 108 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "MJPG", 112 + 1) 'FourCC code to indicate this is a motion jpeg file.
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 116 + 1) 'Flags for pallete changes etc. Usually zero no pallet changes
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 120 + 1) 'stream priority - usually set to zero if it has only 1 stream
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 124 + 1) 'Specifies how far audio is skewed ahead - video only set to zero
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(1000000 / FramesPerSecond), 128 + 1) 'dwScale - calculate this using formula shown below
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(1000000), 132 + 1) 'dwRate - always seems to be set to 1000000. Note that dwRate / dwScale = Frames per second
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 136 + 1) 'Start delay - usually zero start immediately
		'  Put #3, 140 + 1, 0                   'TOTAL NUMBER OF FRAMES IN FILE. SET THIS LATER
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 144 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &HFFFFFFFF, 148 + 1) '*************can be omitted****************
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 152 + 1) 'Sample size, if set to zero samples may vary
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 156 + 1) 'Destination rectangle, only used for multiple video streams, so set to zero here
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 160 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 164 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 168 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "strf", 172 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 68, 176 + 1) 'Size of following structure hex 00000044 = decimal 68 bytes
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 68, 180 + 1) 'Size of this structure in bytes hex 44 = decimal 68 bytes
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, FrameWidth2, 184 + 1) 'Image Width
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, FrameHeight2, 188 + 1) 'Image Height
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 1572865, 192 + 1) 'Number of planes = 1 : 24 Bit colour ie hex 00 18 00 01
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "MJPG", 196 + 1) 'Type of compression used ie. Motion jpeg
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, CInt(FrameWidth2 * FrameHeight2), 200 + 1) 'biSizeImage size of the image in bytes. For jpegs its the size of the buffer. seems to be set at framewidth x frameheight
		'  Put #3, 204 + 1,
		'  Put #3, 208 + 1,
		'  Put #3, 212 + 1,
		'  Put #3, 216 + 1,
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H2C, 220 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H18, 224 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 0, 228 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H2, 232 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H8, 236 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H2, 240 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, &H1, 244 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "JUNK", 248 + 1)
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "Animation Created on Helium Frog Software written by Martin Price.   See www.HeliumFrog.com", 256 + 1) 'Add my own advert in the junk header
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, 2292, 252 + 1)
		
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "LIST", 2548 + 1)
		'  Put #3, 2552 + 1, 3456                 'DATA LENGTH TO BE SET AS YOU ADD FRAMES
		'UPGRADE_WARNING: Put was upgraded to FilePut and has a new behavior. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"'
		FilePut(3, "movi", 2556 + 1)
		FileClose(3)
	End Sub
End Module